<div class="form">
	<div class="role_form_left">
	
<?php 
Yii::app()->clientScript->registerCoreScript('jquery');
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'adminhtml-role-form',
    //'method'=>'get',
    //'enableAjaxValidation'=>false,
)); /* @var $form CActiveForm */?>

	<p class="note">标识 <span class="required">*</span> 为必填项.</p>

	<?php echo Yii::app()->admin->showPutMsg(); ?>
	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'role_label'); ?><br/>
		<?php echo $form->textField($model,'role_label',array('size'=>20,'maxlength'=>20)); ?>
		<?php echo $form->error($model,'role_label'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'role_name'); ?><br/>
		<?php echo $form->textField($model,'role_name',array('size'=>20,'maxlength'=>20)); ?>
		<?php echo $form->error($model,'role_name'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'p_id'); ?><br/>
		<?php echo $form->dropDownList($model,'p_id', AdminhtmlHelper::getRoleOptions($model->role_id)  ); ?>
		<?php echo $form->error($model,'p_id'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'create_time'); ?><br/>
		<?php echo $form->textField($model,'create_time', array('disabled'=>true,)); ?>
		<?php echo $form->error($model,'create_time'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'update_time'); ?><br/>
		<?php echo $form->textField($model,'update_time', array('disabled'=>true,)); ?>
		<?php echo $form->error($model,'update_time'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'status'); ?><br/>
		<?php echo $form->dropDownList($model,'status', AdminhtmlHelper::getStatusOptions()  ); ?>
		<?php echo $form->error($model,'status'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'acl_desc'); ?><br/>
		<select id="acl_type">
			<option value="full">全部权限</option>
			<option value="custom">定制权限</option>
		</select>
		<input type="hidden" value="" id="AdminhtmlRole_acl_desc" name="AdminhtmlRole[acl_desc]">
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

	<?php $this->endWidget(); ?>
	</div><!-- form -->

	<div id="acl_custom_div" class="role_form_right">
	<form id="custom_form" action="/yii/dkcps/index.php?r=adminhtml/role/update&id=1" method="post">
	<?php $this->beginWidget('ext.ECheckBoxTree.ECheckBoxTree', array(
		'id'=>'adminhtml-role-tree',
		'htmlOptions'=>array(
			'collapseImage'=> Yii::app()->getBaseUrl(). '/images/adminhtml/treeRightArrow.gif',
			'expandImage'=> Yii::app()->getBaseUrl(). '/images/adminhtml/treeRightArrow.gif',
			'onCheck'=> '{node:"expand"}',
			'onUncheck'=> '{node:"collapse"}',
		),
	)); ?>
	<ul></ul>
	<?php echo AdminhtmlHelper::getRoleTreeHtml( $model->acl_desc ); ?>
	
	<?php $this->endWidget() ?></form>
	</div><div class="clear"></div>

<script type="text/javascript">
function setFullAccessCheckbox(){
  $("input[type='checkbox']").each(function(){
    $(this).attr('checked',true);
  });
  $('#acl_custom_div').hide();
  $('#AdminhtmlRole_acl_desc').val('<?php echo Yii::app()->params['fullAccess']?>');
  //$('#acl_type').val('full');
}
$('#acl_type').change( 
  function (){
    if($('#acl_type').val() =='full'){
	  setFullAccessCheckbox();
    }
    if($('#acl_type').val() =='custom') {
	  $('#acl_custom_div').show();
    }
  }
);
//implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
        // simple data type
        if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    }
    else {
        // recurse array or object
        var n, v, json = [], arr = (obj && obj.constructor == Array);
        for (n in obj) {
            v = obj[n]; t = typeof(v);
            if (t == "string") v = '"'+v+'"';
            else if (t == "object" && v !== null) v = JSON.stringify(v);
            json.push((arr ? "" : '"' + n + '":') + String(v));
        }
        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
};
$.fn.serializeObject = function() {
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return JSON.stringify(o);
};
$("input[type='checkbox']").click(function(){
  $('#AdminhtmlRole_acl_desc').val($('#custom_form').serializeObject());
});
$("input[type='checkbox']").change(function(){
  $('#AdminhtmlRole_acl_desc').val($('#custom_form').serializeObject());
});

var acl_desc= '<?php echo $model->acl_desc ?>';
if(acl_desc=='<?php echo Yii::app()->params['fullAccess']?>'){
  setFullAccessCheckbox();
}else{
  $('#acl_type').val('custom');
}

</script>
</div>
